<?php

class SliderPhotoController extends AdminController
{

	public $path = '';

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$this->path=getcwd ().'/slider/';

		$model=new SliderPhoto;

		if (isset($_POST['SliderPhoto'])) {
			$model->attributes = $_POST['SliderPhoto'];

			$file = CUploadedFile::getInstance($model, 'src');

			$name = time() . '.' . $file->getExtensionName();

			$model->src = $name;

			if ($model->save()) {
				$file->saveAs($this->path . $name);

				$this->redirect($this->createUrl('/admin/sliderPhoto/admin'));
			}
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->path=getcwd ().'/slider/';

		$model=$this->loadModel($id);

		$file=$model->src;

		if($model->delete()){
			if (file_exists($this->path.$file)) {
					unlink($this->path.$file);
				}
		}

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax'])){
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new SliderPhoto('search');
		$model->unsetAttributes();  // clear any default values

		if(isset($_GET['SliderPhoto'])){
			$model->attributes=$_GET['SliderPhoto'];
		}

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return SliderPhoto the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=SliderPhoto::model()->findByPk($id);

		if($model===null){
			throw new CHttpException(404,'The requested page does not exist.');
		}

		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param SliderPhoto $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='slider-photo-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
